package keeto.com.vn.classified.ui;

import keeto.com.vn.classified.config.IConstant;
import keeto.com.vn.classified.config.SettingKey;
import keeto.com.vn.classified.core.ClassifiedCore;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.BitmapAjaxCallback;
import com.google.code.microlog4android.LoggerFactory;
import com.google.code.microlog4android.config.PropertyConfigurator;

public class MainApplication extends Application{
	public static final boolean DEBUG = false;

	public static boolean autoStart = true;
	public static boolean mRingtoneSound = false;

	public static int screenWidth = 0;
	public static int screenHeight = 0;
	public static String authenKey = "";

	@Override
	public void onCreate() {
		ClassifiedCore.getInstance(this).registerReceiver();
		AjaxCallback.setNetworkLimit(8);
		BitmapAjaxCallback.setIconCacheLimit(20);
		BitmapAjaxCallback.setCacheLimit(40);
		BitmapAjaxCallback.setMaxPixelLimit(2000000);

		super.onCreate();
		SharedPreferences preferences = this.getSharedPreferences(
				SettingKey.SHARED_PREFERENCE_NAME, Context.MODE_WORLD_READABLE);
		screenWidth = preferences.getInt(IConstant.SCREEN_WIDTH_KEY, 0);
		screenHeight = preferences.getInt(IConstant.SCREEN_HEIGHT_KEY, 0);
		SharedPreferences sp = getApplicationContext().getSharedPreferences(
				SettingKey.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		//		authenKey = sp.getString(SettingKey.SP_AUTHKEY_NEW, "b96d727f217e233ed2be6467865c2084");
		initLogConfig();

	}

	@Override
	public void onTerminate() {
		super.onTerminate();
		closeLog();
		ClassifiedCore.getInstance(this).unregisterReceiver();
	}

	public static boolean isAutoStart(){
		return autoStart;
	}

	public static void setAutoStart(boolean aAutoStart){
		autoStart = aAutoStart;
	}


	/**
	 * @category 初始化Log
	 * */
	private void initLogConfig() {
		PropertyConfigurator.getConfigurator(this.getApplicationContext())
		.configure();
	}

	/**
	 * @category 关闭Log
	 * */
	private void closeLog() {
		LoggerFactory.shutdown();
	}

	public static int getIMTheme() {
		return android.R.style.Theme_Light_NoTitleBar;
	}


	public static boolean gesturesEnabled() {
		return true;
	}

	public static int getScreenWidth() {
		return screenWidth;
	}

	public static void setScreenWidth(int screenWidth) {
		MainApplication.screenWidth = screenWidth;
	}

	public static int getScreenHeight() {
		return screenHeight;
	}

	public static void setScreenHeight(int screenHeight) {

		MainApplication.screenHeight = screenHeight;
	}

	public static String getAuthenKey() {
		if(authenKey.length() > 0){
			return authenKey;
		}else{
			return "";
		}
	}

	public static void setAuthenKey(String authenKey) {
		if (authenKey == null){
			MainApplication.authenKey = "";
		}else{
			MainApplication.authenKey = authenKey;
		}
	}
	
	@Override
	public void onLowMemory() {
		// TODO Auto-generated method stub
		super.onLowMemory();
		BitmapAjaxCallback.clearCache();
	}
}
